<template>
  <div class="login">
    <div style="width: 500px;height: 300px; margin: 200px 0 0 600px;">
      <h1 style="text-align: center; margin: 100px 0 50px 100px;">管理员登录</h1>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="ruleForm.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
      <div style="text-align: center;">
        <el-link href="/sys-admin/">后台管理（临时，不需要登录即可跳转）</el-link>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        username: '',password:''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url='http://localhost:8090/admins/login';
          console.log('尝试登录...');
          console.log('请求路径为：'+url);
          console.log('请求参数为：'+this.ruleForm);
          console.log(this.ruleForm);
          let formData=this.qs.stringify(this.ruleForm);
          this.axios.post(url,formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response);
            let responseData = response.data;
            if (responseData.code==20000){
              this.$message({
                message: '登录成功！',
                type: 'success'
              });
              //获取服务器端响应的jwt,并保存下来
              let jwt=responseData.data;
              console.log("服务端响应的jwt为：");
              console.log(jwt);
              localStorage.setItem('jwt',jwt);
              console.log('已经将jwt数据保存到localStorage中了');
              let localJwt=localStorage.getItem('jwt');
              console.log('从localStorage中取出的jwt为：');
              console.log(localJwt);
              //跳转到后台主页
              this.$router.push("/sys-admin")
            }else if (response.data==2){
              this.$notify.error({
                title: '登录失败',
                message: '密码错误！'
              });
            }else {
              this.$notify.error({
                title: '登录失败',
                message: '用户名不存在！'
              });
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
body {
  background-color: #04c9fa !important;
}

.login {
  background-image: url("/public/images/bg.jpg");
}
</style>